
#library(haven)
#library(devtools)
#install_github("pewresearch/pewmethods", build_vignettes = TRUE)
#library(pewmethods)
#library(tidyverse)
dat <- read.csv("~/Desktop/replication/sample_for_weighting.csv")

# These need to be specified for the User
outdir_tables <-"~/Desktop/replication/"
outdir_figures <-"~/Desktop/replication/"

# 1 = Donor, 2= High Income, 3 = Gen Pop

########################################################################
#       High Net Worth Sample
#########################################################################

dat$resp.indx <- seq(1,nrow(dat))
rownames(dat) <- dat$resp.indx

dat <- dat[dat$sample==2,]

dat$ts_genderstring[is.na(dat$ts_genderstring)] <- ""
dat$ts_race_black[is.na(dat$ts_race_black)] <- ""
dat$tsd_registeredvote[is.na(dat$tsd_registeredvote)] <- ""
dat$tsd_regdem[is.na(dat$tsd_regdem)] <- ""
dat$tsd_regrep[is.na(dat$tsd_regrep)] <- ""
dat <- dat[-as.logical(dat$tsd_regdem=="")*seq(1,nrow(dat)),]

nrow(dat)
sum(dat$completedsurvey,na.rm=TRUE)
mean(dat$completedsurvey[dat$tsd_regrep=="yes"],na.rm=TRUE)
mean(dat$completedsurvey[dat$tsd_regdem=="yes"],na.rm=TRUE)

table(dat$ts_fec_tot_cont==0)
table(dat$ts_fec_tot_cont==0)/nrow(dat)

age.quartiles <- summary(dat$ts_age)
age.target <- age.quartiles[c(2,3,5)]

dat$w.age <- NULL
dat$w.age[dat$ts_age < age.target[1]] <- "<53"
dat$w.age[dat$ts_age >= age.target[1] & dat$ts_age < age.target[2]] <- "53-64"
dat$w.age[dat$ts_age >= age.target[2] & dat$ts_age < age.target[3]] <- "64-73"
dat$w.age[dat$ts_age >= age.target[3]] <- "73-100"
dat$w.age[is.na(dat$ts_age)] <- "Missing"
dat$w.age <- as.factor(dat$w.age)

partisan.target <- summary(dat$ts_tsmart_partisan_score)[c(2,3,5)]
dat$party.score <- NULL
dat$party.score[dat$ts_tsmart_partisan_score <= partisan.target[1]] <- "<=6"
dat$party.score[dat$ts_tsmart_partisan_score > partisan.target[1] & dat$ts_tsmart_partisan_score < partisan.target[2]] <- "7-39"
dat$party.score[dat$ts_tsmart_partisan_score >= partisan.target[2] & dat$ts_tsmart_partisan_score < partisan.target[3]] <- "40-92"
dat$party.score[dat$ts_tsmart_partisan_score >= partisan.target[3]] <- "93+"
dat$party.score[is.na(dat$ts_tsmart_partisan_score)] <- "Missing"
dat$party.score <- as.factor(dat$party.score)


dat$ts_genderstring <- as.factor(dat$ts_genderstring)

dat$w.ts.wealth <- NULL
dat$w.ts.wealth[dat$ts_wealth=="Below $25,000" | dat$ts_wealth=="$25,000 - $49,999" | dat$ts_wealth=="$50,000 - $74,999" | dat$ts_wealth=="$75,000 - $99,999"] <- " < $100,000"
dat$w.ts.wealth[dat$ts_wealth=="$100,000 - $149,999" | dat$ts_wealth=="$150,000 - $199,999"] <- "$100,000 - $199,999"
dat$w.ts.wealth[dat$ts_wealth=="$200,000 - $249,999" | dat$ts_wealth=="$250,000 - $499,999"] <- "$200,000 - $499,999"
dat$w.ts.wealth[dat$ts_wealth=="$500,000 - $749,999" | dat$ts_wealth=="$750,000 - $999,999"] <- "$500,000 - $999,999"
dat$w.ts.wealth[dat$ts_wealth=="$1,000,000 - $2,499,999"] <- "$1,000,000 - $2,499,999"
dat$w.ts.wealth[dat$ts_wealth=="$2,500,000 - $4,999,999" | dat$ts_wealth=="$5,000,000 - $9,999,999" | dat$ts_wealth=="Above $10,000,000"] <- ">$2,500,000"
dat$w.ts.wealth[dat$ts_wealth=="Missing"] <- "Missing"

# Make factors for weighting

dat$tsd_regrep <- as.factor(dat$tsd_regrep)
dat$tsd_regdem <- as.factor(dat$tsd_regdem)
dat$tsd_registeredvote <- as.factor(dat$tsd_registeredvote)
dat$ts_race_black <- as.factor(dat$ts_race_black)
dat$w.ts.wealth <- as.factor(dat$w.ts.wealth)
dat$ts_vote_2016_presprimary <- as.factor(dat$ts_vote_2016_presprimary)
dat$ts_vote_2016_general <- as.factor(dat$ts_vote_2016_general)
dat$ts_vote_2018_general <- as.factor(dat$ts_vote_2018_general)

# Population Targets for Appendix Table

###############################################################################################################
# Appendix Table G2: Population Targets for Appendix Table
###############################################################################################################

table(dat$w.age)/nrow(dat)*100
table(dat$tsd_regdem)/nrow(dat)*100
table(dat$tsd_regrep)/nrow(dat)*100
table(dat$party.score)/nrow(dat)*100
table(dat$ts_genderstring)/nrow(dat)*100
table(dat$ts_race_black)/nrow(dat)*100
table(dat$w.ts.wealth)/nrow(dat)*100
table(dat$ts_vote_2016_general)/nrow(dat)*100
table(dat$ts_vote_2016_presprimary)/nrow(dat)*100
table(dat$ts_vote_2018_general)/nrow(dat)*100

dat.Wealth <- dat

targets <- create_raking_targets(
  dat.Wealth,
  vars = c("w.age","ts_genderstring","party.score","tsd_regrep","tsd_regdem","ts_race_black","w.ts.wealth","ts_vote_2016_general","ts_vote_2018_general","ts_vote_2016_presprimary"),
  prefix = "",
  wt = 1,
)

# Propensity Score
prop.score <- glm(completedsurvey~tsd_regrep + tsd_regdem + w.ts.wealth + ts_race_black + ts_genderstring + w.age + ts_vote_2016_general + ts_vote_2016_presprimary + ts_vote_2018_general + party.score,
                  data=dat.Wealth,family=binomial(),x=TRUE)

p.score <- round(prop.score$fitted.values,digits=4)
resp.indx <- as.numeric(names(p.score))
Prop.Dat <- as.data.frame(cbind(resp.indx,p.score))
colnames(Prop.Dat) <- c("resp.indx","PropensityWgt")

########################################################################################
#     Create Weight for Wealthy Sample to Match Population Targets of Wealthy
#########################################################################################

dat.Wealth.complete <- dat.Wealth[dat.Wealth$completedsurvey==1,]
wgt.Wealth <- rake_survey(dat.Wealth.complete,pop_margins = targets)

dat.Wealth.complete <- merge(dat.Wealth.complete,Prop.Dat,by="resp.indx",all.x=TRUE)

###############################################################################################################
# Appendix Table G2: Sample Demographics for Appendix Table
###############################################################################################################

# Sample Demographics for Appendix Table
nrow(dat.Wealth.complete)
table(dat.Wealth.complete$w.age)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$tsd_regdem)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$tsd_regrep)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$party.score)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$ts_genderstring)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$ts_race_black)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$w.ts.wealth)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$ts_vote_2016_general)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$ts_vote_2016_presprimary)/nrow(dat.Wealth.complete)*100
table(dat.Wealth.complete$ts_vote_2018_general)/nrow(dat.Wealth.complete)*100

